﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Block : MonoBehaviour
{
    public LayerMask groundLayer;
    public bool isDead;
    
    public Rigidbody2D rb;
    public BoxCollider2D collider;
    public Animator animator;
    void Start()
    {
        
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 0, groundLayer);
            if (hit.collider != null && hit.collider.gameObject == this.collider.gameObject)
            {
                Debug.Log("自己: " + hit.collider.gameObject.name);
                Dead();
            }
        }
    }

    public void Dead()
    {
        if (isDead == false)
        {
            isDead = true;
            animator.SetBool("play", true);

            Dictionary<string, object> Params = new Dictionary<string, object>()
                {{"isOpen", true}, };
            NotifyEvent e = new NotifyEvent("EVENT_Block_Dead", Params, this);
            NotifacitionCenter.GetInstance().postNotification(e);

            Debug.Log("发送-EVENT_Block_Dead");
        }
    }

    public void AnimationComplete()
    {
        //GetComponent<Animator>().SetBool("play", false);
        animator.enabled = false;
        //collider.isTrigger = true;
        Check();

        Destroy(gameObject);
    }

    public void Check()
    {
        RaycastHit2D[] hits_up = ShuaiFunctions.RaycastAll(transform.position, Vector2.zero, Vector2.up, 1, groundLayer, Color.red); //长度1的射线
        RaycastHit2D[] hits_down = ShuaiFunctions.RaycastAll(transform.position, Vector2.zero, Vector2.down, 1, groundLayer, Color.red); //长度1的射线
        RaycastHit2D[] hits_left = ShuaiFunctions.RaycastAll(transform.position, Vector2.zero, Vector2.left, 1, groundLayer, Color.red); //长度1的射线
        RaycastHit2D[] hits_right = ShuaiFunctions.RaycastAll(transform.position, Vector2.zero, Vector2.right, 1, groundLayer, Color.red); //长度1的射线

        if (hits_up.Length >= 2)
        {
            Debug.Log("上 ==> " + hits_up[1].collider.name);
            hits_up[1].collider.gameObject.GetComponent<Block>().Dead();
        }
        if (hits_down.Length >= 2)
        {
            Debug.Log("下 ==> " + hits_down[1].collider.name);
            hits_down[1].collider.gameObject.GetComponent<Block>().Dead();
        }
        if (hits_left.Length >= 2)
        {
            Debug.Log("左 ==> " + hits_left[1].collider.name);
            hits_left[1].collider.gameObject.GetComponent<Block>().Dead();
        }
        if (hits_right.Length >= 2)
        {
            Debug.Log("右 ==> " + hits_right[1].collider.name);
            hits_right[1].collider.gameObject.GetComponent<Block>().Dead();
        }
    }

    

    //void OnTriggerEnter2D(Collider2D collision)
    //{
    //    Debug.Log("进入- " + collision);

    //    if (collision.gameObject.tag == "Block")
    //    {

    //    }
    //}

    //void OnTriggerExit2D(Collider2D collision)
    //{
    //    Debug.Log("离开- " + collision);

    //    if (collision.gameObject.tag == "Block")
    //    {

    //    }
    //}
}